package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        //判断密码是否已经设置，有的话进行加密
        //彩虹表md5
        String password = member.getPassword();
        //加密明文密码
        if (password != null) {
            //使用MD5加密
            password = MD5Utils.md5(password);
            //加盐salt  salt是随机值
            member.setPassword(password);
        }

        memberMapper.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> month) {

        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m=m+".31";
            Integer count = memberMapper.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }
}
